package com.leezk.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leezk.order.constant.CommonConstant;
import com.leezk.order.dto.ResultDTO;
import com.leezk.order.entity.Student;
import com.leezk.order.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.Map;
import java.util.PrimitiveIterator;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @PostMapping("/register")
    public ResultDTO<Boolean> registerStudent(@RequestBody Student student){
        student.setCreateTime(new Date());
        return ResultDTO.success(studentService.registerStudent(student));
    }

    @GetMapping("/login")
    public ResultDTO<Map<String,String>> loginStudent(String username, String password){
        return ResultDTO.success(studentService.login(username,password));
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> deleteStudent(int id){
        return ResultDTO.success(studentService.removeById(id));
    }

    @PostMapping("/update")
    public ResultDTO<Boolean> updateStudent(@RequestBody Student student){
        return ResultDTO.success(studentService.updateById(student));
    }

    @GetMapping("/getStudent")
    public ResultDTO<Page<Student>> getStudent(int pageNum,int pageSize){
        Page<Student> page=new Page<>(pageNum,pageSize);
        return ResultDTO.success(studentService.getStudent(page));
    }

    @GetMapping("/getStudentById")
    public ResultDTO<Student> getStudent(int id){
        return ResultDTO.success(studentService.getById(id));
    }
}
